package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import cn.wolfcode.trip.base.service.IInformationService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private IInformationService informationService;

    @Override
    public PageInfo query(CommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Comment> list = commentMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Comment comment) {
        if (comment.getId() != null) {
            commentMapper.updateByPrimaryKey(comment);
        } else {
            //设置当前用户
            comment.setUser(UserContext.getCurrentUser());
            commentMapper.insert(comment);
            /**
             * 插入评论后,我们把信息以及信息和用户的关系,保存在数据库中.
             */
            if (comment.getParentId()!=null){
                Comment comment1 = commentMapper.selectByPrimaryKey(comment.getParentId());
                Information information = new Information();
                User author = comment1.getUser();
                User currentUser = UserContext.getCurrentUser();
                information.setUser(currentUser);
                information.setType(Information.USER_MESSAGE_COMMENT);
                information.setTitle("评论");
                information.setContent(currentUser.getNickName() + "给您的评论评论了,内容是"+comment.getContent());
                informationService.saveUserInformation(information, author);
            }
        }
    }

    @Override
    public List<Comment> listAll() {
        return null;
    }

    @Override
    public Comment get(Long id) {
        return null;
    }

    @Override
    public void saveOrUpdate(Comment comment, String[] tags) {

    }

    @Override
    public Integer queryCountByStrategyCommentId(Long strategyCommentId) {
        return commentMapper.queryCountByStrategyCommentId(strategyCommentId);
    }
}
